var win = Titanium.UI.currentWindow;

var db = Titanium.Database.open ('dagboksDB');

var data = [];
 
var rows = db.execute('SELECT * FROM child');
while (rows.isValidRow())
{
   data.push(Ti.UI.createTableViewRow({title:rows.fieldByName('NAME'), childId:rows.fieldByName('ID'), width:180, color:"black"}));
   rows.next();
}

rows.close();

var chooseLabel = Ti.UI.createLabel({
  title:'Choose who to post to',
  height:35,
  left:25,
  top:0
});
win.add(chooseLabel);

var tableview = Titanium.UI.createTableView({
  data:data, 
  backgroundColor:"#f6f6f6", 
  color:"black", 
  height:110, 
  left:22,
  top:34, 
  width:180,
  borderRadius:4,
  borderWidth:1,
  borderColor:"#ccc"
});
win.add(tableview);

var postView = Ti.UI.createView ({
  top:139,
  height:'auto',
  width:'auto',
  layout:'vertical'
});

var rubrikLabel = Ti.UI.createLabel({
  title:'Rubrik',
  top:0,
  left:25,
  height:35,
  width:200
});
postView.add(rubrikLabel);

var rubrikInput = Ti.UI.createTextField({
    color:'#336699',
    height:40,
    top:0,
    left:20,
    width:250,
    borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
})
postView.add(rubrikInput);

var textLabel = Ti.UI.createLabel({
  title:'Dagboks text',
  top:0,
  left:25,
  height:35,
  width:200
});
postView.add(textLabel);

var textInput = Ti.UI.createTextArea({
    color:'#336699',
    height:105,
    top:0,
    left:20,
    width:250,
    borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
})
postView.add(textInput);

var postButton = Ti.UI.createButton({
  title:'Post',
  top:20,
  left:25,
  width:75,
  height:35
});
postView.add(postButton);

// *************************************** FIXA IMAGEN ************************************//
var imageLabel = Ti.UI.createLabel({
  title:'Image',
  top:0,
  right:58,
  height:35
});
win.add(imageLabel);

var imagen = win.image;

var imageView = Ti.UI.createImageView({
  top:34,
  width:80,
  height:110,
  right:20,
  borderRadius:4,
  borderWidth:1,
  borderColor:"#ccc",
  image:imagen
});
    
win.add(imageView);

// *************************************** FIXA IMAGEN ************************************//
    
win.add(postView);

//win.open();

var checkRow;
var childId;

tableview.addEventListener('click', function(e){
  if(checkRow){
    checkRow.hasCheck = false;
  }
  e.row.hasCheck=true;
  checkRow = e.row; 
  childId = e.rowData.childId;
});


//notification pop-up
var notice = Ti.UI.createNotification({message:"Post successfully added."});
// Set the duration to either Ti.UI.NOTIFICATION_DURATION_LONG or NOTIFICATION_DURATION_SHORT
notice.duration = Ti.UI.NOTIFICATION_DURATION_SHORT;

// Setup the X & Y Offsets
notice.offsetX = 0;
notice.offsetY = 20;


postButton.addEventListener('click', function(e)
{
var rubrikvalue = rubrikInput.value;
var textvalue = textInput.value;
  if( rubrikvalue && textvalue && childId)
  {
    db.execute('INSERT INTO dagbok (RUBRIK, POST, CHILDID) VALUES(?,?,?)', rubrikvalue, textvalue, childId);

      var win2 = Ti.UI.createWindow({
      url:'viewall.js',
      title:'All posts',
      backgroundColor:"black",
            
      });
    notice.show()
    win2.open({modal:true});
  }else{
    alertDialog1.show();
  }
});
//db.close();

var alertDialog1 = Titanium.UI.createAlertDialog({
    title: 'Fail!',
    message: 'Please complete the form, you twat!',
    buttonNames: ['OK, sorry....']
});


var scrollView = Titanium.UI.createScrollView({
    contentWidth:'auto',
    contentHeight:'auto',
    top:0,
    showVerticalScrollIndicator:true,
    showHorizontalScrollIndicator:true
});

var view = Ti.UI.createView({
    backgroundColor:'black',
    borderRadius:10,
    width:300,
    height:2000,
    top:10
});

postView.add(view);
postView.add(scrollView);


var menu = Ti.UI.Android.OptionMenu.createMenu();
  Ti.include("menu.js");
